package com.cy.sports.controller;


import com.cy.sports.entity.OssProperties;
import com.cy.sports.entity.Result;
import com.cy.sports.service.mpl.OSSServiceImpl;
import com.cy.sports.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Garne
 * @since 2021-05-01
 */
@Api(value = "OSS", tags = "阿里云OSS服务", description = "用户测试接口")
@RestController
@RequestMapping("/oss")
public class OssController {
    @Autowired
    OSSServiceImpl iossService;

    @Autowired
    OssProperties ossProperties;

    /**
     * 阿里云 oss web直传
     * 验证签名:
     *
     * @return
     */
    @GetMapping("/getSign")
    @ApiOperation("OSS签名")
    public Map<String, String> callbackSign() {
        Map<String, String> sign = iossService.getSign(ossProperties);
        return sign;
    }

//    /**
//     * 阿里云  oss 回调进行回调:
//     *
//     * @return
//     */
//    @RequestMapping("/callbackUrl")
//    public void callbackUrl(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String ossCallbackBody = iossService.GetPostBody(request.getInputStream(),
//                Integer.parseInt(request.getHeader("content-length")));
//        boolean b = iossService.VerifyOSSCallbackRequest(request, ossCallbackBody);
//        if (b) {
//            ossStsAuth.response(request, response, "{\"Status\":\"OK\"}", HttpServletResponse.SC_OK);
//        } else {
//            ossStsAuth.response(request, response, "{\"Status\":\"verdify not ok\"}", HttpServletResponse.SC_BAD_REQUEST);
//        }
//    }

    /**
     * @Description OSS上传回调
     * @Author
     * @Param [ossCallbackBody, authorization, publicKeyUrlBase64, request, response]
     * @Return com.ejsino.chat.config.domain.JSONResult
     * @return
     */
    @PostMapping("/ossCallBack")
    @ApiOperation("OSS上传回调")
    public Result callBack(@RequestBody String ossCallbackBody, @RequestHeader("Authorization") String authorization,
                                       @RequestHeader("x-oss-pub-key-url") String publicKeyUrlBase64, HttpServletRequest request,
                                       HttpServletResponse response) {
        boolean isCallBack = iossService.verifyOSSCallbackRequest(authorization, publicKeyUrlBase64, ossCallbackBody, request.getQueryString(), request.getRequestURI());
        if (isCallBack) {
            response.setStatus(HttpServletResponse.SC_OK);
            // 此处必须使用json形式返回，不然oss回调报 <Message>Response body is not valid json format.</Message>
            return ResultUtil.success("success");
        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            // 此处必须使用json形式返回，不然oss回调报 <Message>Response body is not valid json format.</Message>
            return ResultUtil.fail("error");
        }
    }
}
